import Toast from '@vant/weapp/toast/toast';
import {addUserInfo} from "../../api/userInfo"

const arrList = [
  {
    values:[
      "1栋","2栋","3栋","4栋","5栋","6栋","7栋","8栋","9栋","10栋"
    ],
    className:"dong_id"
  },
  {
    values:[
      "1楼","2楼","3楼","4楼","5楼","6楼","7楼","8楼","9楼","10楼",
      "11楼","12楼","13楼","14楼","15楼","16楼","17楼","18楼","19楼","20楼",
      "21楼","22楼","23楼","24楼"
    ],
    className:"lou_id"
  },
  {
    values:["第1户","第2户","第3户","第4户","第5户","第6户"],
    className:"hu_id"
  }
]

Page({
  /**
   * 页面的初始数据
   */
  data: {
    ownerName:"",
    phoneNumber:"",
    gender:0 | 1 | 2, // 0 对应女性 1 对应男性 2 对应保密,
    address:"请选择家庭住址",
    checkInTime:"请选择入住时间",
    familyInfo:null,
    showAddressPopup:false,
    showCheckInTimePopup:false,
    columns:arrList,
    currentDate:new Date().getTime(),
    minDate: new Date().getTime(),
  },

  onLoad(options){
    // 将注册后的手机号保存到用户信息中
    this.setData({
      phoneNumber:options.phoneNumber
    })
  },

  // 改变性别
  genderChange(event){
    this.data.gender = event.detail
  },

  // 控制地址弹窗是否弹出
  controlAddressPopup(){   
    this.setData({
      showAddressPopup:!this.data.showAddressPopup
    })
  },

  // 更改家庭地址
  confirmAddress(event){
    let address = ""
    event.detail.value.forEach(item => {
      address += item
    })

    this.setData({
      address,
      showAddressPopup:!this.data.showAddressPopup
    })


  },

  // 确认并更新入住时间
  controlCheckInTimePopup(){
    this.setData({
      showCheckInTimePopup:!this.data.showCheckInTimePopup
    })
  },

  // 确认入住时间
  confirmCheckInTimePopup(event){
    let timestamp = event.detail // 时间戳
    let date = new Date(timestamp)
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()

    this.setData({
      showCheckInTimePopup:!this.data.showCheckInTimePopup,
      checkInTime:year+"年"+month+"月"+day+"日"
    })
  },

  // 校验用户输入有效性
  check(ownerName,gender,address,checkInTime){
    if(ownerName === ""){
      return false
    }else if(gender > 2 || gender < 0){
      return false
    }else if(address === "请选择家庭住址"){
      return false
    }else if(checkInTime === "请选择入住时间"){
      return false
    }else{
      return true
    }
  },

  // 提交
  async submit(){
    let {phoneNumber,ownerName,gender,address,checkInTime,familyInfo} = this.data
    console.log(!this.check())
    if(!this.check(ownerName,gender,address,checkInTime)){
      Toast({
        type:"fail",
        message:"有信息待填写"
      })
    }else{
      let ret = await addUserInfo({phoneNumber,ownerName,gender,address,checkInTime,familyInfo})
      if(ret.code === 200){
        wx.switchTab({
          url: '/pages/index/index',
        })
      }else{
        Toast({
          type:"fail",
          message:"网络异常"
        })
      }
    }
  }
})